package siscom.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Carlos Ribeiro
 */
public class Util {

    public static boolean isConectadoInternet() {
        try {
        	//TODO muito lento quando internet esta com problemas
        	Util.logaDebug("TESTANDO INTERNET...");
            URL url = new URL("http://www.google.com.br");
            URLConnection conn = url.openConnection();
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setConnectTimeout(5000);
            httpConn.setReadTimeout(5000);
            if (httpConn.getResponseCode() == httpConn.HTTP_OK) {
                return true;
            }
            return false;

        } catch (IOException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public static void trataErro(Exception e) {
    	e.printStackTrace();
        DialogTrataErro dlg = new DialogTrataErro(null, true, e);
        dlg.setVisible(true);

    }
    
    public static void setLookAndFeel(JFrame frame) {
        try {
            UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
            if(frame == null ) {
                return;
            }
            SwingUtilities.updateComponentTreeUI(frame);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Date formataStringToDate(String data) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Date dataHe = null;
        try {
            dataHe = sdf.parse(data);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dataHe;
    }

    public static Date getDataAtual() {
        Calendar calendar = new GregorianCalendar();
        Date date = new Date();
        calendar.setTime(date);
        return calendar.getTime();
    }
    
    public static void logaDebug(String texto) {
    	if(!Configuracoes.isDebugOn()) {
    		return;
    	}
    	System.out.println("-----LOG---DEBUG----");
    	System.out.println(texto);
    	System.out.println("-----FIM-LOG-DEBUG--");
    }
    
    public static void loga(String texto) {
    	System.out.println("-----LOG-----");
    	System.out.println(texto);
    	System.out.println("--FIM-LOG----");
    }
    
    public static TipoSistemaOperacional getSistemaOperacional() {
        return TipoSistemaOperacional.WINDOWS;
    }
}
